package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;


public class FriendAgreeTaskProcess extends SetTaskExtra {
    private static FriendAgreeTaskProcess ourInstance = new FriendAgreeTaskProcess();

    public static FriendAgreeTaskProcess getInstance() {
        return ourInstance;
    }

    private FriendAgreeTaskProcess() {
    }


    private int mSelectPosition = -1;
    private Random mRandom = new Random();

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {

        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            LogManager.getInstance().logInfo("intab = " + inDiscover);
            if (inDiscover) {
                View friendView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.DISCOVER_LIST_ITEM_ID, "朋友圈");
                LogManager.getInstance().logInfo("friendView = " + friendView);
                if (null != friendView) {
                    ViewOperation.clickView(friendView);
                }
            }
            mSelectPosition = -1;
        } else if (WxUiViewConfig.CIRCLE_OF_FRIENDS.equals(contextName)) {

            View listView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_LIST_VIEW);
            LogManager.getInstance().logInfo("listView = " + listView);
            if (null != listView && listView instanceof ListView) {
                ListView list = (ListView) listView;
                if(list.getCount()==2)
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "当前微信号 里  还没有朋友圈");
                if(list.getAdapter().getCount()>=3) {
                    if (mSelectPosition < 0) {
                        mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 2) + list.getHeaderViewsCount();
                    }
                    int first = list.getFirstVisiblePosition();
                    int last = list.getLastVisiblePosition();
                    LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + list.getCount() + " footCount = " + list.getFooterViewsCount());
                    if ((mSelectPosition==1&&last==1)||(mSelectPosition < first || mSelectPosition > last)) {
                        ViewOperation.moveListPosition(list, mSelectPosition);
                        LogManager.getInstance().logInfo("不到位");
                    } else {
                        LogManager.getInstance().logInfo("移动到位");
                        View  firstView=null;
                        if(first==0)
                            firstView = list.getChildAt(1);
                        else if(mSelectPosition==list.getAdapter().getCount()-2)
                            firstView = list.getChildAt(last-first-1);
                        else
                            firstView = list.getChildAt(0);
                        if (null != firstView) {
                            View agreeView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_AGREE);
                            LogManager.getInstance().logInfo("agreeView = " + agreeView);
                            if (null != agreeView && agreeView.getVisibility() == View.VISIBLE) {
                                TextView textView = (TextView) agreeView;
                                if ("取消".equals(textView.getText().toString())) {
                                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "点赞成功");
                                } else if ("赞".equals(textView.getText().toString())) {
                                    ViewOperation.clickView(agreeView);
                                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "点赞成功");
                                }
                            } else {
                                View menuView = firstView.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_OPEN_MENU);
                                if (null != menuView) {
                                    ViewOperation.clickView(menuView);
                                }
                                LogManager.getInstance().logInfo("menuView=" + menuView);
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}
